
#include "S_16_SingleSmartScene_doSet.h"
#include "string.h"
#include "mi_iid_define.h"
#include "spec.h"
#include "main.h"
#include "section1_set_func.h"

//uint8_t g_AutoModeState = 0;

static void P_16_1_AutoCooling_doSet(property_operation_t *o)
{
	char *Temp = NULL;
	char *Buf[4] = {0};
	uint16_t Data[2] = {0};
	uint8_t Cnt = 0;	
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_STRING)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

    // TODO: 执行写操作: o->value.data.string.value;

	LOG_INFO("%s, %s\r\n",__func__,o->value.data.string.value);
	
    // 如果成功，返回代码: OPERATION_OK
    o->code = OPERATION_OK;
	Temp = strtok(o->value.data.string.value,","); 
	while((Temp != NULL) && (Cnt < 3)) 
	{
		Buf[Cnt] = Temp;
		Cnt ++;
		Temp = strtok(NULL, ",");
	}
	Data[0] = (uint16_t)(atof(Buf[1]) *10);
	Data[1] = (uint16_t)(atof(Buf[2]) *10);
	PRINTF("%s %s %s %d %d\r\n",Buf[0],Buf[1],Buf[2],Data[0],Data[1]);
	if(strcmp(Buf[0],"true") == 0)
	{
		A34_16_1_Set(true,Data[0],Data[1]);
	}
	else if(strcmp(Buf[0],"false") == 0)
	{
		A34_16_1_Set(false,Data[0],Data[1]);
	}
	
	
//	g_AutoModeState = AUTO_COOL;
//	BeepStart();
    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_16_1_AutoCooling_doChange_notify(o->value.data.string.value);

    return;
}

static void P_16_2_AutomaticHeating_doSet(property_operation_t *o)
{
	char *Temp = NULL;
	char *Buf[4] = {0};
	uint16_t Data[2] = {0};
	uint8_t Cnt = 0;		
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_STRING)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

    // TODO: 执行写操作: o->value.data.string.value;

	LOG_INFO("%s, %s\r\n",__func__,o->value.data.string.value);
	
    // 如果成功，返回代码: OPERATION_OK
    o->code = OPERATION_OK;
		Temp = strtok(o->value.data.string.value,","); 
		while((Temp != NULL) && (Cnt < 3)) 
		{
			Buf[Cnt] = Temp;
			Cnt ++;
			Temp = strtok(NULL, ",");
		}
		Data[0] = (uint16_t)(atof(Buf[1]) *10);
		Data[1] = (uint16_t)(atof(Buf[2]) *10);
		PRINTF("%s %s %s %d %d\r\n",Buf[0],Buf[1],Buf[2],Data[0],Data[1]);
		if(strcmp(Buf[0],"true") == 0)
		{
			A3A_16_2_Set(true,Data[0],Data[1]);
		}
		else if(strcmp(Buf[0],"false") == 0)
		{
			A3A_16_2_Set(false,Data[0],Data[1]);
		} 
//		CalculateModeIndex(HOTMODE);//计算模式Index
		
//		g_AutoModeState = AUTO_HOT;
		
//		BeepStart();		
    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_16_2_AutomaticHeating_doChange_notify(o->value.data.string.value);
		
    return;
}

static void P_16_3_AutoDry_doSet(property_operation_t *o)
{
	char *Temp = NULL;
	char *Buf[4] = {0};
	uint16_t Data[2] = {0};
	uint8_t Cnt = 0;	
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_STRING)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

    // TODO: 执行写操作: o->value.data.string.value;

	LOG_INFO("%s, %s\r\n",__func__,o->value.data.string.value);
	
    // 如果成功，返回代码: OPERATION_OK
    o->code = OPERATION_OK;
		Temp = strtok(o->value.data.string.value,","); 
		while((Temp != NULL) && (Cnt < 3)) 
		{
			Buf[Cnt] = Temp;
			Cnt ++;
			Temp = strtok(NULL, ",");
		}
		Data[0] = (uint16_t)(atof(Buf[1]) *10);
		Data[1] = (uint16_t)(atof(Buf[2]) *10);
		PRINTF("%s %s %s %d %d\r\n",Buf[0],Buf[1],Buf[2],Data[0],Data[1]);
		if(strcmp(Buf[0],"true") == 0)
		{
			A40_16_3_Set(true,Data[0],Data[1]);
		}
		else if(strcmp(Buf[0],"false") == 0)
		{
			A40_16_3_Set(false,Data[0],Data[1]);
		}     
//		CalculateModeIndex(DEHUMIDIFYMODE);//计算模式Index
//		BeepStart();
    //上报状态，通知app状态变化，统一APP修改、定时器触发后的上报机制
//    P_16_3_AutoDry_doChange_notify(o->value.data.string.value);

    return;
}

static void P_16_12_ResetDefaltfilter_doSet(property_operation_t *o)
{
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value.format != PROPERTY_FORMAT_BOOLEAN)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

    // TODO: 执行写操作: o->value.data.boolean.value;

	LOG_INFO("%s, %d\r\n",__func__,o->value.data.boolean.value);
	
    // 如果成功，返回代码: OPERATION_OK
    o->code = OPERATION_OK;
		
  A07_16_12_Set(o->value.data.boolean.value);  
//	BeepStart();
		
    return;
}

void S_16_SingleSmartScene_doSet(property_operation_t *o)
{
    switch (o->piid)
    {
        case PIID_16_1_AutoCooling:
            P_16_1_AutoCooling_doSet(o);
            break;

        case PIID_16_2_AutomaticHeating:
            P_16_2_AutomaticHeating_doSet(o);
            break;

        case PIID_16_3_AutoDry:
            P_16_3_AutoDry_doSet(o);
            break;

        case PIID_16_12_ResetDefaltfilter:
            P_16_12_ResetDefaltfilter_doSet(o);
            break;

        default:
            o->code = OPERATION_ERROR_VALUE;
            break;
    }
}
